﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

using Microsoft.Phone.Controls;


namespace WP7SignalRHelperClient
{
    public partial class ObjSync : PhoneApplicationPage
    {
        #region "Members"

        ObjectSyncClient phoneObjToSync = new ObjectSyncClient();
        BackgroundWorker SyncBackgroundDataWorker = new BackgroundWorker();

        #endregion

        #region "Constructor"

        public ObjSync()
        {
            InitializeComponent();            

            // Set up the background workers for sync.
            SyncBackgroundDataWorker.WorkerSupportsCancellation = false;
            SyncBackgroundDataWorker.WorkerReportsProgress = false;
            SyncBackgroundDataWorker.DoWork += new DoWorkEventHandler(SyncBackgroundDataWorker_DoWork);
        }

        #endregion

        #region "Event Handlers"

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Instantiate with default or any other implementation.
            App.Current.SignalRHub = new SignalRMessagingHub();

            // Wire-up to listen to custom event from SignalR Hub.
            App.Current.SignalRHub.SignalRServerNotification += new SignalRServerHandler(SignalRHub_SignalRServerNotification);
           
            phoneObjToSync.PhoneClientId = App.Current.DeviceID;

            // Send to server to do object sync.
            App.Current.SignalRHub.StartObjectSync(phoneObjToSync);          
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // Unwire.
            App.Current.SignalRHub.SignalRServerNotification -= new SignalRServerHandler(SignalRHub_SignalRServerNotification);
        }

        protected void SignalRHub_SignalRServerNotification(object sender, SignalREventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                // Update object properties on phone.
                prop1Txt.Text = e.CustomObject.Property1.ToString();
                prop2Txt.Text = e.CustomObject.Property2.ToString();
                prop3Chk.IsChecked = e.CustomObject.Property3;
            }));
        }

        private void syncBtn_Click(object sender, EventArgs e)
        {            
            phoneObjToSync.PhoneClientId = App.Current.DeviceID;
            phoneObjToSync.CustomObject = new CustomClass();
            phoneObjToSync.CustomObject.Property1 = prop1Txt.Text.Trim();
            phoneObjToSync.CustomObject.Property2 = Convert.ToInt32(prop2Txt.Text.Trim());
            phoneObjToSync.CustomObject.Property3 = (bool)prop3Chk.IsChecked;

            // Fire off background thread to do object sync with server.               
            SyncBackgroundDataWorker.RunWorkerAsync();            
        }

        private void SyncBackgroundDataWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Send to server to do object sync.
            App.Current.SignalRHub.DoObjectSync(phoneObjToSync);
        }

        #endregion
    }
}